package com.example.controller.msg;

import com.alibaba.fastjson.JSONObject;
import com.example.auth.domain.User;
import com.example.config.auth.SecurityUtil;
import com.example.config.exception.GlobalExcetion;
import com.example.domain.vo.ReceiveMsgVo;
import com.example.feign.UserFeignService;
import com.example.ribbon.service.ReceiveMsgRecordRibbonService;
import com.example.ribbon.service.ReceiveMsgRibbonService;
import com.example.util.Results;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("receive")
public class ReceiveMsgController {

    @Autowired
    private UserFeignService userFeignService;

    @Autowired
    private ReceiveMsgRibbonService receiveMsgRibbonService;

    @Autowired
    private ReceiveMsgRecordRibbonService receiveMsgRecordRibbonService;

    @PostMapping("sendmsg")
    public JSONObject sendMsg(@Validated @RequestBody ReceiveMsgVo vo) throws GlobalExcetion {
        User toUser=userFeignService.getUserByUserId(vo.getToUid());
        receiveMsgRibbonService.receiveMsg(vo.getToUid(), vo.getMsg());
        receiveMsgRecordRibbonService.receiveMsgRecord(vo.getToUid(), vo.getMsg());
        return Results.success("已发送给成功:"+toUser.getUsername());
    }

    @GetMapping("/getmsglist")
    public JSONObject getMsgList(){
        return receiveMsgRibbonService.getUserMsgList();
    }

    @GetMapping("/getmsglistpage")
    public JSONObject getMsgList(@RequestParam(value = "pageNum",defaultValue = "1")Integer pageNum,
                                 @RequestParam(value = "pageSize",defaultValue = "10")Integer pageSize){
        return receiveMsgRibbonService.getUserMsgPage(pageNum,pageSize);
    }

    @GetMapping("/getmsglist/page")
    public JSONObject getMsgListPage(@RequestParam(value = "pageNum",defaultValue = "1")Integer pageNum,
                                     @RequestParam(value = "pageSize",defaultValue = "10")Integer pageSize,
                                     @RequestParam(value = "formUid")String formUid){
        return receiveMsgRibbonService.getUserMsgPage(pageNum,pageSize,formUid);
    }
}
